
#include "alkes/effector/GaussianFilter.h"
#include "TestGaussianFilter.h"

#include <stdio.h>


TestGaussianFilter::TestGaussianFilter()
: factory_(0)
{
}

TestGaussianFilter::~TestGaussianFilter()
{
}

void TestGaussianFilter::setUp()
{
    factory_ = alkes::ImageBufferFactory::create();
}

void TestGaussianFilter::tearDown()
{
    factory_ = 0;
}

void TestGaussianFilter::testGenerateFilter()
{
    alkes::intrusive_ptr<alkes::GaussianFilter> filter(alkes::GaussianFilter::create(0, 0, 0.0f));

    // failed to create filter.

    filter = alkes::GaussianFilter::create(factory_, 0, 0.0f);

    // failed to create filter.

    filter = alkes::GaussianFilter::create(factory_, 1, 0.0f);

    // failed to create filter.

    filter = alkes::GaussianFilter::create(factory_, 3, 1.0f);
    filter = alkes::GaussianFilter::create(factory_, 3, 0.5f);
    filter = alkes::GaussianFilter::create(factory_, 3, 0.849321800288f);
    filter = alkes::GaussianFilter::create(factory_, 5, 0.849321800288f);
}

void TestGaussianFilter::testApplyFilter()
{
    alkes::intrusive_ptr<alkes::GaussianFilter> filter(alkes::GaussianFilter::create(factory_, 3, 0.849321800288f));

    alkes::intrusive_ptr<alkes::ImageBuffer> ibuffer(factory_->createImageBuffer(5, 5, AL_PIXEL_FORMAT_A8));

    int32_t pitch, x, y;
    void* src = ibuffer->lockRect(0, alkes::AL_LOCKMODE_WRITE, &pitch);
    uint8_t* p;
    {
        for (y = 0; y < 5; ++y)
        {
            p = (uint8_t*)src + y * pitch;
            for (x = 0; x < 5; ++x, ++p)
            {
                *p = 0xff;
            }
        }
        ibuffer->unlockRect();
    }


    alkes::intrusive_ptr<alkes::ImageBuffer> result(0);
    bool ret = filter->apply(ibuffer, &result);

    uint16_t w, h;
    result->getImageSize(w, h);
    AL_ASSERT(w == 7);
    AL_ASSERT(h == 7);
    AL_ASSERT(ret);

    src = result->lockRect(0, alkes::AL_LOCKMODE_READ, &pitch);
    for (y = 0; y < 7; ++y)
    {
        p = (uint8_t*)src + y * pitch;
        for (x = 0; x < 7; ++x, ++p)
        {
            AL_DBG("(%d,%d)=%02x", x, y, *p);
        }
        ibuffer->unlockRect();
    }

    result = 0;
    ibuffer = 0;
    filter = 0;
}
